﻿using ciji.Application.Dto;
using ciji.Core;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ciji.Application.Data
{
   
    /// <summary>
    /// 系统设置
    /// </summary>
    [ApiDescriptionSettings("系统设置", Order = 900, Description = "系统设置接口")]
 
    public class SystemService : IDynamicApiController
    {

        private SqlSugarRepository<sys_ActionLog> _actrionLogRep;
        private SqlSugarRepository<sys_LoginLog> _loginLogRep;
        private SqlSugarRepository<sys_ErrorLog> _errorLogRep;
        private SqlSugarRepository<sys_OperateLog> _operateLogRep;
        private SqlSugarRepository<sys_Log> _sysLogRep;
        

        public SystemService(SqlSugarRepository<sys_ActionLog> actrionLogRep
            , SqlSugarRepository<sys_LoginLog> loginLogRep
            , SqlSugarRepository<sys_ErrorLog> errorLogRep
            , SqlSugarRepository<sys_OperateLog> operateLogRep
            , SqlSugarRepository<sys_Log> sysLogRep) { 
            _actrionLogRep = actrionLogRep;
            _loginLogRep = loginLogRep;
            _errorLogRep = errorLogRep;
            _operateLogRep = operateLogRep;
            _sysLogRep = sysLogRep;

        

        }

        #region 操作日志

       
        //保存导出日志
        public void SaveActionLog(sys_ActionLog actionlog)
        {
            actionlog.Gid = Guid.NewGuid().ToString(); 
             _actrionLogRep.Insert(actionlog);
           
        }

        /// <summary>
        /// 查看导出日志
        /// </summary>     
        /// <returns></returns>

        [HttpPost]
        public SqlSugarPagedXList<sys_ActionLog> SearchActionLog(SearchInputDto input )
        {

            return _actrionLogRep.AsQueryable().WhereIF(!string.IsNullOrEmpty(input.employeeid),m=>m.sys_Creater==input.employeeid)
                .ToPagedList(input.page,input.size);
             
        } 
        /// <summary>
        /// 查看操作日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        [HttpPost]
        public SqlSugarPagedXList<sys_OperateLog> SearchOperateLog(SearchInputDto input)
        {
            return _operateLogRep.AsQueryable().OrderByDescending(m => m.Id)
               .ToPagedList(input.page, input.size);
        }
         
        /// <summary>
        /// 收集错误日志
        /// </summary>     
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public void SaveErrorLog(sys_ErrorLog errorlog)
        {
           
            _errorLogRep.Insert(errorlog);
        }

        /// <summary>
        /// 查看错误日志
        /// </summary>     
        /// <returns></returns>

        [HttpGet]
        public List<sys_ErrorLog> GetErrorLog()
        {
            return _errorLogRep.AsQueryable().OrderByDescending(m => m.Id)
                .ToList();
        }

        /// <summary>
        /// 查看登录日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        [HttpPost]
        public SqlSugarPagedXList<sys_LoginLog> SearchLoginLog(SearchInputDto input)
        {
            return _loginLogRep.AsQueryable().OrderByDescending(m => m.Id)
               .ToPagedList(input.page, input.size);
        }

        /// <summary>
        /// 查看日志
        /// </summary>     
        /// <returns></returns>

        [HttpGet]
        public List<sys_Log> GetLog(string level)
        {
            return _sysLogRep.AsQueryable().WhereIF(!string.IsNullOrEmpty(level), m => m.Level == level)
                .OrderByDescending(m => m.Id).ToList();
        }

        /// <summary>
        /// 删除错误日志
        /// </summary>     
        /// <returns></returns>
         
        public void DeleteErrorLog()
        {
            _errorLogRep.Delete(m=>true);
        }
        /// <summary>
        /// 删除日志
        /// </summary>     
        /// <returns></returns>

        
        public void DeleteLog()
        {
            _sysLogRep.Delete(m => true); 
        }
        #endregion
    }
}
